package com.tjshop.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.tjshop.android.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tjshop.android.GoodsListActivity;
import com.tjshop.android.GoodsDetailActivity;
import com.tjshop.android.fragment.AbstractItem;
import com.tjshop.android.model.ShopInfo;
import com.tjshop.android.view.PinnedSectionListView.PinnedSectionListAdapter;

import java.util.List;

/**
 * Created by Administrator on 2014/10/21.
 */
public class shopCoupon_adapter extends BaseAdapter implements
		PinnedSectionListAdapter {

	private LayoutInflater inflater;
	List<ShopInfo> couponList;
	private Context mContext;

	public shopCoupon_adapter(Context context, List<ShopInfo> clist,
			LayoutInflater inflater) {
		this.inflater = inflater;
		couponList = clist;
		this.mContext = context;
	}

	@Override
	public int getCount() {
		return couponList.size() * 2;
	}

	static class Item {
		public int type;
		public static final int TYPE_HEAD = 0;
		public static final int TYPE_SECTION = 1;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public Item getItem(int arg0) {
		Item item = new Item();
		if (arg0 % 2 == 0) {
			item.type = Item.TYPE_HEAD;
		} else {
			item.type = Item.TYPE_SECTION;
		}
		return item;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public int getItemViewType(int position) {
		return getItem(position).type;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		if (arg0 % 2 == 0) {
			convertView = inflater.inflate(
					R.layout.inc_activity_main_fragment_nearby_list_item, null);
			TextView shopName = (TextView) convertView
					.findViewById(R.id.shopName);
			TextView shopArea = (TextView) convertView
					.findViewById(R.id.shopArea);
			TextView shopDistance = (TextView) convertView
					.findViewById(R.id.shopDistance);
			TextView coupon_count = (TextView) convertView
					.findViewById(R.id.coupon_count);
			shopName.setText(couponList.get(arg0 / 2).getShop_name());
			shopArea.setText(couponList.get(arg0 / 2).getShop_area());
			float distance = 0;
			distance = couponList.get(arg0 / 2).getDistance() / 1000000;
			shopDistance.setText(distance + "km");
			coupon_count
					.setText("点击查看"
							+ couponList.get(arg0 / 2).getCoupon_lists().size()
							+ "个团购");
			return convertView;
		} else {
			ListView listView = new ListView(mContext);
			listView.setAdapter(new ShopListChildAdapter(mContext, couponList
					.get(arg0 / 2).getCoupon_lists(), inflater));

			return listView;
		}

	}

	@Override
	public boolean isItemViewTypePinned(int viewType) {
		return viewType == Item.TYPE_HEAD;
	}

}
